package com.yulinlin.websocket.node.consume;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.RedisSerializer;

public abstract class RedisConsumer<E> implements MessageListener {

    @Autowired
    private RedisSerializer<Object> redisSerializer;


    @Override
    public void onMessage(Message message, byte[] bytes) {


        String channel = new String(message.getChannel());

        Object data =  redisSerializer.deserialize(message.getBody());
        if(getChannel().equals(channel)){
            run((E)data);
        }
    }

    protected abstract void run(E data);

    public abstract String getChannel();



}
